package com.erp.produce.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.produce.dao.CustomerDao;
import com.erp.produce.entity.Customer;
import com.erp.produce.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;


    @Override
    public Boolean update(Customer customer) {
        Customer customer1 = customerDao.findById(customer.getId()).get();
        BeanUtils.copyProperties(customer,customer1);

        return customerDao.saveAndFlush(customer) != null;
    }

    @Override
    public Boolean add(Customer customer) {
        return customerDao.save(customer) != null;
    }

    @Override
    @Transactional
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,customerDao);
    }

    @Override
    public List<Customer> listPage(int page, int size) {
        return customerDao.findAll(PageRequest.of(page, size)).getContent();
    }

    @Override
    public List<Customer> listPageLike(int page, int size, String column) {
        return customerDao.findAllByCustomerNameLike(StringUtil.addLike(column), PageRequest.of(page, size));
    }

    @Override
    public List<Customer> listOrderById() {
        return customerDao.findAll(new Sort(Sort.Direction.ASC,"id"));
    }

}
